package com.test.boot.http.rpc.server.config;

import com.test.boot.http.rpc.api.service.HelloService;
import com.test.boot.http.rpc.server.service.HelloServiceImp;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
import org.springframework.web.context.support.HttpRequestHandlerServlet;

/**
 * Created by hujianjun on 2015/12/4.
 */
@Configuration
public class HelloServiceServletConfig {

    @Bean
    public ServletRegistrationBean helloServletRegistrationBean() {
        HttpRequestHandlerServlet servlet = new HttpRequestHandlerServlet();
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
        servletRegistrationBean.setServlet(servlet);
        servletRegistrationBean.setName("helloServiceExporter");        //映射为bean的名字
        servletRegistrationBean.addUrlMappings("/hello");
        servletRegistrationBean.setEnabled(true);
        return servletRegistrationBean;
    }

    @Bean
    public HelloService helloService() {
        HelloService service = new HelloServiceImp();
        return service;
    }

    @Bean(name="helloServiceExporter")
    public HttpInvokerServiceExporter httpInvokerServiceExporter() {
        HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
        exporter.setServiceInterface(HelloService.class);
        exporter.setService(helloService());      // 设置对应的实例，该实例会作为参数注入进来
        return exporter;
    }
}
